package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Course;
import com.example.demo.entity.Course;
import com.example.demo.mapper.CourseMapper;
import com.example.demo.service.ICourseService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


/**
 * @descreption:
 * @author: xhr
 * @date: 2022/9/03
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    CourseMapper courseMapper;
    @Resource
    private ICourseService courseService;
    //增
    @PostMapping
    public Result<?> save(@RequestBody Course course) {

        courseMapper.insert(course);
        return Result.success();
    }
    // 改
    @PutMapping
    public Result<?> update(@RequestBody Course course) {
        courseMapper.updateById(course);
        return Result.success();
    }
    // 删
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        courseMapper.deleteById(id);
        return Result.success();
    }
    //为其他功能提供Select 选择器数组进行课程筛查。
//    @GetMapping("/cour")
//    public Result<?> findAll2() {
//        return Result.success(courseMapper.selectList(new QueryWrapper<Course>().select("id","name")));
//    }
    //全查（给其他vue用）
    @GetMapping
    public Result<?> findAll(){
        return Result.success(courseService.list());
    }
    //全(页)查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Course> wrapper = Wrappers.<Course>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Course::getName, search);
        }
        return Result.success(courseMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

}
